﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IRCPhase2Tester.Daemons;

namespace IRCPhase2Tester.Tests
{
    public class UserTable : MultiClientsMultiDaemons
    {
        #region ITest Members

        public override string Title()
        {
            return "User Table";
        }

        public override void BuildUp()
        {
            StartNewDaemon(1, Session.Instance.AllConfigurationFiles[1], typeof(ExternalRoutingDaemon));
            StartNewDaemon(2, Session.Instance.AllConfigurationFiles[2], typeof(ExternalRoutingDaemon));
            StartNewDaemon(3, Session.Instance.AllConfigurationFiles[3], typeof(ExternalRoutingDaemon));
            StartNewClient(this.daemons[0].IRCLocalPort);
            StartNewClient(this.daemons[1].IRCLocalPort);
            StartNewClient(this.daemons[2].IRCLocalPort);

            base.BuildUp();
        }

        public override bool RunTest()
        {
            clients[0].Send("adduser test1");
            if (clients[0].Receive().ToLower().Trim() != "ok")
            {
                return false;
            }

            clients[1].Send("adduser test2");
            if (clients[1].Receive().ToLower().Trim() != "ok")
            {
                return false;
            }

            clients[2].Send("adduser test3");
            if (clients[2].Receive().ToLower().Trim() != "ok")
            {
                return false;
            }

            System.Threading.Thread.Sleep(5000);

            int attempts = 5;
            while (attempts-- > 0)
            {
                clients[0].Send("usertable");
                string response = clients[0].Receive().ToLower().Trim();
                string[] lines = response.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

                bool success = true;

                if (lines.Length == 3)
                {
                    for (int i = 0; i < lines.Length; i++)
                    {
                        switch (i)
                        {
                            case 0:
                                if (lines[i].Trim() != "ok 2")
                                    success = false;
                                break;
                            case 1:
                            case 2:
                                if (lines[i].Trim() != "test2 2 1")
                                {
                                    if (lines[i].Trim() != "test3 3 1")
                                        success = false;
                                }
                                break;
                        }
                    }
                }
                else
                    success = false;

                if (success)
                    return true;

                System.Threading.Thread.Sleep(10000);
            }

            return false;
        }

        #endregion
    }
}
